/*  
 * Copyright 2007 MBARI 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.mbari.timeline.piccolo;

import org.mbari.timeline.DateUtil;
import org.mbari.timeline.piccolo.Timeline;

import junit.framework.TestCase;

import com.domainlanguage.time.TimeInterval;
import com.domainlanguage.time.TimePoint;

/** 
 * @author Andrew Chase
 */
public class TestTimeline extends TestCase {
	TimeInterval range;
	Timeline timeLine;
	
	public void setUp(){
		//These starting times are no longer valid because the timeline now has a constraint
		//that start and end times must be at midnight.
		/*
		 * TimePoint start = DateUtil.ansiStandardDateStringToDate("20070416-22:22:01");
	     * TimePoint end = DateUtil.ansiStandardDateStringToDate("20070426-22:22:01");
	    */
		//The dates within the tests are still computed using the dates above. all the test
		//dates need to be adjusted to match these new dates.
		TimePoint start = DateUtil.ansiStandardDateStringToDate("20070416-00:00:00");
	    TimePoint end = DateUtil.ansiStandardDateStringToDate("20070426-00:00:00");
		range = new TimeInterval(start, true, end, true);
		timeLine = new Timeline(range);
		timeLine.setWidth(100);
	}
	
	public void testXCoordinateToDate() throws Exception{
		TimePoint expectedDate = DateUtil.ansiStandardDateStringToDate("20070421-00:00:00");
		//there will be some fudge in the translation from double to long
		long difference = absTimeDifference(expectedDate, timeLine.xCoordinateToDate(50.0));
		assertTrue("difference is: " + difference, difference < 500);
	}
	
	public void testDateToFractionalXCoordinate() throws Exception{
	    TimePoint dateToConvert = DateUtil.ansiStandardDateStringToDate("20070422-16:12:00");
	    timeLine.setWidth(100);
        double x = timeLine.dateToXCoordinate(dateToConvert);
        double difference = Math.abs(x - 66.666);
        assertTrue("difference is: " + difference, difference < .1);
    }	    
	
	private long absTimeDifference(TimePoint date1, TimePoint date2){
		return Math.abs(date1.asJavaUtilDate().getTime() - date2.asJavaUtilDate().getTime());
	}
}
